package com.sqx.modules.pay.util;

import java.io.UnsupportedEncodingException;
import java.security.PrivateKey;
import java.security.PublicKey;

import com.jyt.baseUtil.des.CryptoUtils;
import com.jyt.baseUtil.utils.StringUtil;

public class RSAUtil {

	public static  RSAHelper rsaHelper = new RSAHelper();


	private static final String privateStr =
			"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC8YiBQBznsvOCx\n" +
			"14hqg8L5SwMBvUAXVitGKhZ/6gD1W1jTFyhCPgB8eqSG5UUmgbOJIOlt92q35iOn\n" +
			"l5SS9oOzhxYwTtVA/bVHzXx0ROKBDPRf9+7bYQqKAB4HVwvrYl1x5toOUQPp1diw\n" +
			"AwlIRoMIYJw+MqPi3Q50OexOhLvHI9FmKt3HShKH2+CoKSuKcf9emri7ulQTgmvD\n" +
			"zylWQ4A6C4XJiH2Tx9vOWKhlIUMyEhRBwKHvTCxvGbHcXsD5Z7+d1WCH+BOObh1l\n" +
			"i3mjI4uIXcKVqRUGIGX4i9zalYLoHsYFNw3f4SHdA7FKDnWOKEEI7YY8Lu8Wgtg9\n" +
			"X1MmBI7tAgMBAAECggEAWODS7e763T6fpEhUDCzuKD/Dw2ZOfrQg5pGk2+TDXv8i\n" +
			"ATXDwL0P7arPRPWWknxPADuO5lr89paV69QQPOYhbOonBdOruWX5yU6hkRmO4+QA\n" +
			"x+Zpwwcduc4kXMIs1XkrLToFVwvSFNjXyvMjxf2+hI1QKegqejZhQ2SwoLru/RyR\n" +
			"zpqj6aPNWkBUlAfPzUvw0IYf6lIuj7uty46+4SFlAJ7GCn4RmtvSaXFp898e/3VH\n" +
			"tB8SHXcjRPR+ENzNArC7SWS1ZooYLsgY+JncnjkFpt72pMHk1o8ECOQI1Ic7qw8b\n" +
			"40ZUWrb/dkXeEBrfQSGOPDSRhg0NIwJUS/XljqyKAQKBgQDcCiuYJ2OIjNyeNLJ2\n" +
			"yBHY2JIUhh3XK611NmoLas+a5vhrPKHUoJTHjeHoAHwL6n5+cVrPBzxlv8cumKy5\n" +
			"AtRHm5/Ci/iTJaCwcom8NoGx/Zu3FDGfrv9ZLr593jZqM5Z7XGJPHrRu9kv8iqS4\n" +
			"VQYWfxR72plvBqWRmT1JLQVmfQKBgQDbK4rPKQKhk454ZZWKQpOQmIWwuWcHUmLc\n" +
			"IXW4x3wO51gyyDbFW3m1bOBNf0Ncx9bSNYynBWTABwUf7uPsi8HIu2des98wdLj1\n" +
			"/A6+v5QbYYV1yGZ9JwKgXJRT/G/rLra7PXHsx5JaKxg9zj32e9Nq3yLHhya6ajbE\n" +
			"CfwjxEGFMQKBgCuWQCh1tPu5m4IjQi7RVuezjsxN/N9Aoa2UR9FXKSSwGAl2C0CJ\n" +
			"Rq945YUFg40AuGnnnmtgLZCiOpx6VcyYSOCebTkmbOkJDRmmtq33xh0x9YSAlYvi\n" +
			"KGbMIaPhProu2eI53Kd7l5ZbZ5XxCMwS24/dQHuAQ+CAWo/K4R77shQpAoGBALek\n" +
			"H1X/Vd4O5mSJMai8qiqRMgvm/aYL6RvryVj/hTpzq9o5GnN5d1yGEte8l7PpGl3e\n" +
			"UrEr+sRP3YJpR39Phxrrs+yNKKjQVJ6ACdbYbYAX0ZzkQsQA6Nl3WS2aKijUZnYz\n" +
			"QP8WJdH5pAE9i3u5O2ej+d9XpiKylprihuttKT0xAoGATu621jyiC+aObWqqhF3S\n" +
			"kUGYJJYjW3eZGWrofLaRQI3AwaxVBuO2JpX8vAzWQhzMNhWIpD4d4IYBEhU77V7+\n" +
			"YbZUjY4I7rCNoHWCOEHtXtdDA1HYQaXe+XkwUAV4dS+YARGKU9Hul6Hr0BvDo8fT\n" +
			"AgbvBYr0Q7zYkErOpiZvT+0=";
	private static final String publicStr =
			"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAowYntjDIcKONnvHBq1ps\n" +
			"p9Odi+RqbbOSOrDHYN+XS2rvCHW3jCXbiu9ANbJ6VFsxmbgBrXmwlVLeq+Jk2xcS\n" +
			"+9cwbHvOt59fWswPWpjMmvs7MgSproV8/5IfRlRSdjZZRlUxIed6esuavAV8LpPA\n" +
			"yLdZu3Nn6bhzFpX5/P6sCz1NafE/l62tldBZghYsBjaV1tsZXzJXakYaukNLTc2F\n" +
			"tUjpCvTalpUuM+qgUQ4WbPUutFiStWg+D7508jUSKxpti+Lc/AKXNkOBXq3Xfp4m\n" +
			"D6DCSN8Z7G6kMPLTq2DI9eAEOBGdR7XMg+GAQzB+6w40jLm1PfGLx3W7k2Z8y8hH\n" +
			"LwIDAQAB";

	static{
		try {
//            String pfxPath = RSAUtil.class.getClassLoader().getResource("cert/merchantTest.pfx").getPath();
//            String pubCertPath = RSAUtil.class.getClassLoader().getResource("cert/jytpayserver.cer").getPath();
//            rsaHelper.initKey(rsaHelper.getPrivateKeyFromPfx(pfxPath, "password"), rsaHelper.getPublicKeyFromCer(pubCertPath));

            /**
             * 自建证书
             */
            String privateKeyStr = privateStr;
            String publicKeyStr= publicStr;
            PrivateKey privateKey = rsaHelper.getPrivateKey(privateKeyStr);
            PublicKey publicKey  = rsaHelper.getPublicKey(publicKeyStr);
            rsaHelper.initKey(privateKey, publicKey);
		} catch (Exception e) {
			System.out.println("密钥初始化异常");
			e.printStackTrace();
		}
	}

	public static String signMsg( String xml ){
		String hexSign = null ;

		try {
			byte[] sign = rsaHelper.signRSA(xml.getBytes("UTF-8"), false, "UTF-8") ;

			hexSign = StringUtil.bytesToHexString(sign) ;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return hexSign;
	}

	public static String encryptKey( byte[] key){

		byte[] enc_key = null ;
		try {
			enc_key = rsaHelper.encryptRSA(key, false, "UTF-8") ;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return StringUtil.bytesToHexString(enc_key) ;
	}

	public static  byte[] decryptKey(String hexkey){
		byte[] key = null ;
		byte[] enc_key = StringUtil.hexStringToBytes(hexkey) ;

		try {
			key = rsaHelper.decryptRSA(enc_key, false, "UTF-8") ;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return key ;
	}

	public static String encryptMsg(String msg, byte[] key) {
		String enc_msg = CryptoUtils.desEncryptToHex(msg, key);
		return enc_msg;
	}

	public static String decrytXml(String xml_enc, byte[] key) {
		String xml = CryptoUtils.desDecryptFromHex(xml_enc, key);
		return xml;
	}


	public static boolean verifyMsgSign(String xml, String sign)
	{
		byte[] bsign = StringUtil.hexStringToBytes(sign) ;

		boolean ret = false ;
		try {
			ret = rsaHelper.verifyRSA(xml.getBytes("UTF-8"), bsign, false, "UTF-8") ;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return ret;
	}
}
